<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>模型字段 Field List</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, field-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
    <style type="text/css">
        .layui-btn-dropdown {
            position: relative;
            display: inline-block
        }

        .layui-btn-dropdown .layui-dropdown-menu {
            position: absolute;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            display: none;
            margin: 2px 0 0;
            padding: 5px 0;
            min-width: 120px;
            border: 1px solid #ccc;
            border: 1px solid rgba(0, 0, 0, .15);
            border-radius: 4px;
            background-color: #fff;
            background-clip: padding-box;
            -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
            box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
            list-style: none;
            text-align: left;
            font-size: .875rem;
            -webkit-background-clip: padding-box
        }

        .layui-btn-dropdown .layui-dropdown-menu > li > a {
            clear: both;
            display: block;
            padding: 3px 15px;
            color: #333;
            white-space: nowrap;
            font-weight: 400;
            line-height: 1.42857143
        }

        .layui-btn-dropdown .layui-dropdown-menu > li > a:focus, .layui-btn-dropdown .layui-dropdown-menu > li > a:hover {
            background-color: #f0f0f0;
            text-decoration: none
        }

        .layui-btn-dropdown .layui-dropdown-menu .divider {
            overflow: hidden;
            margin: 9px 0;
            height: 1px;
            background-color: #e5e5e5
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-form layui-card">
                <div class="layui-card-header">
                    模型字段列表
                    <div  class="layui-form-item" style="float: right;margin-top: 2px;">
                        <select name="layui-form-module" id="layui-form-module" lay-filter="layui-form-module-filter" class="form-control input-sm" style="width:auto;">
                            <option value="" >选择模型字段</option>
                            <option th:each="m:${modules}" th:value="${m.code}" th:selected="${m.code == moduleCode}?'true':'false'" th:text="${m.name}"></option>
                        </select>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="field-list-table" lay-filter="field-list-filter"></table>
                    <script type="text/html" id="field-list-table-operate-bar">
                        {{#  if(d.isSystem != '1'){ }}
                        <a shiro:hasPermission="dev:module:field:edit" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a shiro:hasPermission="dev:module:field:remove" class="layui-btn layui-btn-danger layui-btn-xs"
                           lay-event="del">删除</a>
                        {{#  } }}
                    </script>

                    <script type="text/html" id="field-list-table-switch-required-tpl">
                        <input type="checkbox" name="required" lay-skin="switch" lay-filter="field-list-table-required"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.required == 1 ? 'checked' : '' }}/>
                    </script>
                    <script type="text/html" id="field-list-table-switch-listed-tpl">
                        <input type="checkbox" name="listed" lay-skin="switch" lay-filter="field-list-table-listed"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.listed == 1 ? 'checked' : '' }}/>
                    </script>
                    <script type="text/html" id="field-list-table-switch-searched-tpl">
                        <input type="checkbox" name="searched" lay-skin="switch" lay-filter="field-list-table-searched"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.searched == 1 ? 'checked' : '' }}/>
                    </script>

                    <script type="text/html" id="field-list-table-toolbar">
                        <div class="layui-btn-dropdown">
                            <button shiro:hasPermission="dev:module:field:add" class="layui-btn layui-btn-sm"
                                    data-toggle="dropdown" lay-event="toggle">
                                添加字段到模型 <span class="layui-icon" style="font-size: 14px"></span>
                            </button>
                            <ul class="layui-dropdown-menu">
                                <li><a lay-event="add" th:each="m : ${modules}" th:data-id="${m.id}" th:text="${m.type == 1 ? '[单页]' + m.name : '[列表]' + m.name}">NAV</a></li>
                                <!--li class="divider"></li--><!--分割线-->
                            </ul>
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table;

        var formModule = $('#layui-form-module');

        var tableIns = table.render({
            elem: '#field-list-table'
            , url: '/api/field/page'
            , id: 'id'
            , toolbar: '#field-list-table-toolbar'
            , cols: [[
                {field: 'id', width: 60, title: '序号'}
                , {field: 'moduleName', width: 120, title: '模型', sort: true}
                , {field: 'title', width: 120, title: '字段描述'}
                , {field: 'name', width: 120, title: '字段名称'}
                , {
                    field: 'type', width: 130, title: '类型', sort: true, templet: function (d) {
                        if (d.type == 'input') return '单行文本';
                        if (d.type == 'textarea') return '多行文本';
                        if (d.type == 'picture') return '图片上传';
                        if (d.type == 'attachment') return '附件上传';
                        if (d.type == 'datetime') return '日期选择';
                        if (d.type == 'richbox') return '富文本编辑器';
                        if (d.type == 'radiobox') return '单选按钮';
                        if (d.type == 'checkbox') return '多选按钮';
                        if (d.type == 'combobox') return '下拉选择';
                        if (d.type == 'objectlist') return '关联模型';
                    }
                }
                , {field: 'tabName', title: '分组'}
                , {field: 'required', width: 80, fixed: 'right', title: '必填', templet: '#field-list-table-switch-required-tpl'}
                , {field: 'listed', width: 80, fixed: 'right', title: '列表', templet: '#field-list-table-switch-listed-tpl'}
                , {field: 'searched', width: 80, fixed: 'right', title: '搜索', templet: '#field-list-table-switch-searched-tpl'}
                , {field: 'sorting', width: 60, fixed: 'right', title: '排序'}
                , {width: 120, align: 'center', fixed: 'right', toolbar: '#field-list-table-operate-bar'}
            ]]
            , page: true
            , request: {
                pageName: 'pageNum'
                , limitName: 'pageSize'
            }
        });

        //头工具栏事件
        table.on('toolbar(field-list-filter)', function (obj) {
            var checkStatus = table.checkStatus('id');
            switch (obj.event) {
                case 'toggle':
                    $('.layui-btn-dropdown').find('ul').toggle();
                    break;
                case 'add':
                    $('.layui-btn-dropdown').find('ul').hide();
                    var id = $(this).data('id');
                    var index = layer.open({
                        type: 2
                        , title: '添加字段'
                        , content: '/cms/dev/field/add?moduleId=' + id
                        , maxmin: true
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-field-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                if (field.required == 'on') {
                                    field.required = 1;
                                } else {
                                    field.required = 0;
                                }
                                if (field.listed == 'on') {
                                    field.listed = 1;
                                } else {
                                    field.listed = 0;
                                }
                                if (field.searched == 'on') {
                                    field.searched = 1;
                                } else {
                                    field.searched = 0;
                                }

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/field/add',
                                    method: 'post',
                                    data: field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            layer.msg(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    //layer.full(index);
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(field-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/field/removeFlag?id=' + data.id,
                            method: 'put',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '编辑模型字段'
                        , content: '/cms/dev/field/edit?id=' + data.id
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-field-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                if (field.required == 'on') {
                                    field.required = 1;
                                } else {
                                    field.required = 0;
                                }
                                if (field.listed == 'on') {
                                    field.listed = 1;
                                } else {
                                    field.listed = 0;
                                }
                                if (field.searched == 'on') {
                                    field.searched = 1;
                                } else {
                                    field.searched = 0;
                                }
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/field/edit',
                                    method: 'post',
                                    data: field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
                case 'view':
                    layer.open({
                        type: 2
                        , title: '模型信息'
                        , content: '/cms/dev/field/get?id=' + data.id
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['关闭']
                    });
                    break;
            }
        });

        form.on('select(layui-form-module-filter)', function(data){
            tableIns.reload({
                where: {
                    module : formModule.val()
                }
            });
        });

        form.on("switch(field-list-table-required)",function(obj){
            var requiredInt = obj.elem.checked ? 1 : 0;
            updateDate({id : this.value , required : requiredInt});
        });

        form.on("switch(field-list-table-listed)",function(obj){
            var listedInt = obj.elem.checked ? 1 : 0;
            updateDate({id : this.value , listed : listedInt});
        });

        form.on("switch(field-list-table-searched)",function(obj){
            var searchedInt = obj.elem.checked ? 1 : 0;
            updateDate({id : this.value , searched : searchedInt});
        });

        function updateDate(data) {
            layer.load();
            $.ajax({
                url: '/api/field/update',
                method: 'post',
                data: data,
                dataType: 'JSON',
                success: function (res) {
                    layer.closeAll();
                    if (res.code == '0') {
                        layer.msg('数据修改完成!', {icon: 1,time: 2000});
                    } else {
                        layer.msg('ERROR:数据修改错误!', {icon: 2,time: 2000});
                    }
                },
                error: function (err) {
                    layer.closeAll();
                    layer.msg('数据修改错误', {icon: 2,time: 2000});
                }
            });
        }

    });
</script>
</body>
</html>